Skip to main content

CSS or Xpath Selectors

Get the Output Data in JSON format instead of HTML output with the help of Selectors. You can get JSON data by either specifying Xpath or CSS selectors as follows:

The data_selectors should be in proper format and encoded and so we recommend using our API Dashboard to create the accurate code for the request.

Xpath Selectors

As default, every selector you add is considered an xpath selector. So you do not need to specifically mention it as an Xpath selector.

You just need to add &data_selectors=[{ "key": "Links", "value": "//a/@href", "listing_type": "multiple"}]

And for the above data selector, it will get all the links in a list with key name as "Links".

If you want just the first listing, you can remove the "listing_type": "multiple" and just have the following:

&data_selectors=[{ "key": "Links", "value": "//a/@href"}]

For getting images as well, here is an example:

&data_selectors=[{ "key": "Links", "value": "//a/@href"}, { "key": "Images", "value": "//img/@src"}]

If you are having trouble constructing Xpath Selectors, you can even contact our Chat Support and we will help you with it.

import requests

params = {
'apiKey': 'csa44bedf9asdasdas4b84fd3b46504b6c',
'url': 'https://books.toscrape.com/',
'location': 'Auto',
'js_enabled': 'Yes',
'antibot_level': '1',
'data_selectors':"""[{ "key": "Links", "value": "//a/@href"}, { "key": "Images", "value": "//img/@src"}]"""
}

url = 'https://api.getodata.com/'

response = requests.get(url, params=params)
responseData = response.json()
print(responseData)

Output:

{"Links":"index.html","Images":"media/cache/2c/da/2cdad67c44b002e7ead0cc35693c0e8b.jpg"}

CSS Selectors

For getting data through CSS Selectors, you need to just add an extra param name "selector_type=css"

So for example : To get all the links in a list with a key name as "Links", you can add the below param to the request:

&data_selectors=[{ "key": "Links", "value": "a[href]", selector_type="css" ,"listing_type": "multiple"}]

For getting just the first link: you can remove the "listing_type": "multiple" and just have the following:

&data_selectors=[{ "key": "Links", "value": "a[href]", selector_type="css"}]

If you are having trouble constructing CSS Selectors, you can even contact our Chat Support and we will help you with it.

Example:

import requests

params = {
'apiKey': 'csa44bedf9asdasdas4b84fd3b46504b6c',
'url': 'https://books.toscrape.com/',
'location': 'Auto',
'js_enabled': 'Yes',
'antibot_level': '1',
'data_selectors':"""[{ "key": "Links", "value": "a[href]", "selector_type":"css" ,"listing_type": "multiple"}]"""
}

url = 'https://api.getodata.com/'

response = requests.get(url, params=params)
responseData = response.json()
print(responseData)



Output:

[
"<a href=\"index.html\">Books to Scrape</a>",
"<a href=\"index.html\">Home</a>",
"<a href=\"catalogue/category/books_1/index.html\">\n \n Books\n \n </a>",
"<a href=\"catalogue/category/books/travel_2/index.html\">\n \n Travel\n \n </a>",
"<a href=\"catalogue/category/books/mystery_3/index.html\">\n \n Mystery\n \n </a>",
"<a href=\"catalogue/category/books/historical-fiction_4/index.html\">\n \n Historical Fiction\n \n </a>",
"<a href=\"catalogue/category/books/sequential-art_5/index.html\">\n \n Sequential Art\n \n </a>",
"<a href=\"catalogue/category/books/classics_6/index.html\">\n \n Classics\n \n </a>",
"<a href=\"catalogue/category/books/philosophy_7/index.html\">\n \n Philosophy\n \n </a>",
"<a href=\"catalogue/category/books/romance_8/index.html\">\n \n Romance\n \n </a>",
"<a href=\"catalogue/category/books/womens-fiction_9/index.html\">\n \n Womens Fiction\n \n </a>",
"<a href=\"catalogue/category/books/fiction_10/index.html\">\n \n Fiction\n \n </a>",
"<a href=\"catalogue/category/books/childrens_11/index.html\">\n \n Childrens\n \n </a>",
"<a href=\"catalogue/category/books/religion_12/index.html\">\n \n Religion\n \n </a>",
"<a href=\"catalogue/category/books/nonfiction_13/index.html\">\n \n Nonfiction\n \n </a>",
"<a href=\"catalogue/category/books/music_14/index.html\">\n \n Music\n \n </a>",
"<a href=\"catalogue/category/books/default_15/index.html\">\n \n Default\n \n </a>",
"<a href=\"catalogue/category/books/science-fiction_16/index.html\">\n \n Science Fiction\n \n </a>",
"<a href=\"catalogue/category/books/sports-and-games_17/index.html\">\n \n Sports and Games\n \n </a>",
"<a href=\"catalogue/category/books/add-a-comment_18/index.html\">\n \n Add a comment\n \n </a>",
"<a href=\"catalogue/category/books/fantasy_19/index.html\">\n \n Fantasy\n \n </a>",
"<a href=\"catalogue/category/books/new-adult_20/index.html\">\n \n New Adult\n \n </a>",
"<a href=\"catalogue/category/books/young-adult_21/index.html\">\n \n Young Adult\n \n </a>",
"<a href=\"catalogue/category/books/science_22/index.html\">\n \n Science\n \n </a>",
"<a href=\"catalogue/category/books/poetry_23/index.html\">\n \n Poetry\n \n </a>",
"<a href=\"catalogue/category/books/paranormal_24/index.html\">\n \n Paranormal\n \n </a>",
"<a href=\"catalogue/category/books/art_25/index.html\">\n \n Art\n \n </a>",
"<a href=\"catalogue/category/books/psychology_26/index.html\">\n \n Psychology\n \n </a>",
"<a href=\"catalogue/category/books/autobiography_27/index.html\">\n \n Autobiography\n \n </a>",
"<a href=\"catalogue/category/books/parenting_28/index.html\">\n \n Parenting\n \n </a>",
"<a href=\"catalogue/category/books/adult-fiction_29/index.html\">\n \n Adult Fiction\n \n </a>",
"<a href=\"catalogue/category/books/humor_30/index.html\">\n \n Humor\n \n </a>",
"<a href=\"catalogue/category/books/horror_31/index.html\">\n \n Horror\n \n </a>",
"<a href=\"catalogue/category/books/history_32/index.html\">\n \n History\n \n </a>",
"<a href=\"catalogue/category/books/food-and-drink_33/index.html\">\n \n Food and Drink\n \n </a>",
"<a href=\"catalogue/category/books/christian-fiction_34/index.html\">\n \n Christian Fiction\n \n </a>",
"<a href=\"catalogue/category/books/business_35/index.html\">\n \n Business\n \n </a>",
"<a href=\"catalogue/category/books/biography_36/index.html\">\n \n Biography\n \n </a>",
"<a href=\"catalogue/category/books/thriller_37/index.html\">\n \n Thriller\n \n </a>",
"<a href=\"catalogue/category/books/contemporary_38/index.html\">\n \n Contemporary\n \n </a>",
"<a href=\"catalogue/category/books/spirituality_39/index.html\">\n \n Spirituality\n \n </a>",
"<a href=\"catalogue/category/books/academic_40/index.html\">\n \n Academic\n \n </a>",
"<a href=\"catalogue/category/books/self-help_41/index.html\">\n \n Self Help\n \n </a>",
"<a href=\"catalogue/category/books/historical_42/index.html\">\n \n Historical\n \n </a>",
"<a href=\"catalogue/category/books/christian_43/index.html\">\n \n Christian\n \n </a>",
"<a href=\"catalogue/category/books/suspense_44/index.html\">\n \n Suspense\n \n </a>",
"<a href=\"catalogue/category/books/short-stories_45/index.html\">\n \n Short Stories\n \n </a>",
"<a href=\"catalogue/category/books/novels_46/index.html\">\n \n Novels\n \n </a>",
"<a href=\"catalogue/category/books/health_47/index.html\">\n \n Health\n \n </a>",
"<a href=\"catalogue/category/books/politics_48/index.html\">\n \n Politics\n \n </a>",
"<a href=\"catalogue/category/books/cultural_49/index.html\">\n \n Cultural\n \n </a>",
"<a href=\"catalogue/category/books/erotica_50/index.html\">\n \n Erotica\n \n </a>",
"<a href=\"catalogue/category/books/crime_51/index.html\">\n \n Crime\n \n </a>",
"<a href=\"catalogue/a-light-in-the-attic_1000/index.html\"><img src=\"media/cache/2c/da/2cdad67c44b002e7ead0cc35693c0e8b.jpg\" alt=\"A Light in the Attic\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/a-light-in-the-attic_1000/index.html\" title=\"A Light in the Attic\">A Light in the ...</a>",
"<a href=\"catalogue/tipping-the-velvet_999/index.html\"><img src=\"media/cache/26/0c/260c6ae16bce31c8f8c95daddd9f4a1c.jpg\" alt=\"Tipping the Velvet\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/tipping-the-velvet_999/index.html\" title=\"Tipping the Velvet\">Tipping the Velvet</a>",
"<a href=\"catalogue/soumission_998/index.html\"><img src=\"media/cache/3e/ef/3eef99c9d9adef34639f510662022830.jpg\" alt=\"Soumission\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/soumission_998/index.html\" title=\"Soumission\">Soumission</a>",
"<a href=\"catalogue/sharp-objects_997/index.html\"><img src=\"media/cache/32/51/3251cf3a3412f53f339e42cac2134093.jpg\" alt=\"Sharp Objects\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/sharp-objects_997/index.html\" title=\"Sharp Objects\">Sharp Objects</a>",
"<a href=\"catalogue/sapiens-a-brief-history-of-humankind_996/index.html\"><img src=\"media/cache/be/a5/bea5697f2534a2f86a3ef27b5a8c12a6.jpg\" alt=\"Sapiens: A Brief History of Humankind\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/sapiens-a-brief-history-of-humankind_996/index.html\" title=\"Sapiens: A Brief History of Humankind\">Sapiens: A Brief History ...</a>",
"<a href=\"catalogue/the-requiem-red_995/index.html\"><img src=\"media/cache/68/33/68339b4c9bc034267e1da611ab3b34f8.jpg\" alt=\"The Requiem Red\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/the-requiem-red_995/index.html\" title=\"The Requiem Red\">The Requiem Red</a>",
"<a href=\"catalogue/the-dirty-little-secrets-of-getting-your-dream-job_994/index.html\"><img src=\"media/cache/92/27/92274a95b7c251fea59a2b8a78275ab4.jpg\" alt=\"The Dirty Little Secrets of Getting Your Dream Job\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/the-dirty-little-secrets-of-getting-your-dream-job_994/index.html\" title=\"The Dirty Little Secrets of Getting Your Dream Job\">The Dirty Little Secrets ...</a>",
"<a href=\"catalogue/the-coming-woman-a-novel-based-on-the-life-of-the-infamous-feminist-victoria-woodhull_993/index.html\"><img src=\"media/cache/3d/54/3d54940e57e662c4dd1f3ff00c78cc64.jpg\" alt=\"The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/the-coming-woman-a-novel-based-on-the-life-of-the-infamous-feminist-victoria-woodhull_993/index.html\" title=\"The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull\">The Coming Woman: A ...</a>",
"<a href=\"catalogue/the-boys-in-the-boat-nine-americans-and-their-epic-quest-for-gold-at-the-1936-berlin-olympics_992/index.html\"><img src=\"media/cache/66/88/66883b91f6804b2323c8369331cb7dd1.jpg\" alt=\"The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/the-boys-in-the-boat-nine-americans-and-their-epic-quest-for-gold-at-the-1936-berlin-olympics_992/index.html\" title=\"The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics\">The Boys in the ...</a>",
"<a href=\"catalogue/the-black-maria_991/index.html\"><img src=\"media/cache/58/46/5846057e28022268153beff6d352b06c.jpg\" alt=\"The Black Maria\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/the-black-maria_991/index.html\" title=\"The Black Maria\">The Black Maria</a>",
"<a href=\"catalogue/starving-hearts-triangular-trade-trilogy-1_990/index.html\"><img src=\"media/cache/be/f4/bef44da28c98f905a3ebec0b87be8530.jpg\" alt=\"Starving Hearts (Triangular Trade Trilogy, #1)\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/starving-hearts-triangular-trade-trilogy-1_990/index.html\" title=\"Starving Hearts (Triangular Trade Trilogy, #1)\">Starving Hearts (Triangular Trade ...</a>",
"<a href=\"catalogue/shakespeares-sonnets_989/index.html\"><img src=\"media/cache/10/48/1048f63d3b5061cd2f424d20b3f9b666.jpg\" alt=\"Shakespeare's Sonnets\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/shakespeares-sonnets_989/index.html\" title=\"Shakespeare's Sonnets\">Shakespeare's Sonnets</a>",
"<a href=\"catalogue/set-me-free_988/index.html\"><img src=\"media/cache/5b/88/5b88c52633f53cacf162c15f4f823153.jpg\" alt=\"Set Me Free\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/set-me-free_988/index.html\" title=\"Set Me Free\">Set Me Free</a>",
"<a href=\"catalogue/scott-pilgrims-precious-little-life-scott-pilgrim-1_987/index.html\"><img src=\"media/cache/94/b1/94b1b8b244bce9677c2f29ccc890d4d2.jpg\" alt=\"Scott Pilgrim's Precious Little Life (Scott Pilgrim #1)\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/scott-pilgrims-precious-little-life-scott-pilgrim-1_987/index.html\" title=\"Scott Pilgrim's Precious Little Life (Scott Pilgrim #1)\">Scott Pilgrim's Precious Little ...</a>",
"<a href=\"catalogue/rip-it-up-and-start-again_986/index.html\"><img src=\"media/cache/81/c4/81c4a973364e17d01f217e1188253d5e.jpg\" alt=\"Rip it Up and Start Again\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/rip-it-up-and-start-again_986/index.html\" title=\"Rip it Up and Start Again\">Rip it Up and ...</a>",
"<a href=\"catalogue/our-band-could-be-your-life-scenes-from-the-american-indie-underground-1981-1991_985/index.html\"><img src=\"media/cache/54/60/54607fe8945897cdcced0044103b10b6.jpg\" alt=\"Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/our-band-could-be-your-life-scenes-from-the-american-indie-underground-1981-1991_985/index.html\" title=\"Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991\">Our Band Could Be ...</a>",
"<a href=\"catalogue/olio_984/index.html\"><img src=\"media/cache/55/33/553310a7162dfbc2c6d19a84da0df9e1.jpg\" alt=\"Olio\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/olio_984/index.html\" title=\"Olio\">Olio</a>",
"<a href=\"catalogue/mesaerion-the-best-science-fiction-stories-1800-1849_983/index.html\"><img src=\"media/cache/09/a3/09a3aef48557576e1a85ba7efea8ecb7.jpg\" alt=\"Mesaerion: The Best Science Fiction Stories 1800-1849\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/mesaerion-the-best-science-fiction-stories-1800-1849_983/index.html\" title=\"Mesaerion: The Best Science Fiction Stories 1800-1849\">Mesaerion: The Best Science ...</a>",
"<a href=\"catalogue/libertarianism-for-beginners_982/index.html\"><img src=\"media/cache/0b/bc/0bbcd0a6f4bcd81ccb1049a52736406e.jpg\" alt=\"Libertarianism for Beginners\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/libertarianism-for-beginners_982/index.html\" title=\"Libertarianism for Beginners\">Libertarianism for Beginners</a>",
"<a href=\"catalogue/its-only-the-himalayas_981/index.html\"><img src=\"media/cache/27/a5/27a53d0bb95bdd88288eaf66c9230d7e.jpg\" alt=\"It's Only the Himalayas\" class=\"thumbnail\"></a>",
"<a href=\"catalogue/its-only-the-himalayas_981/index.html\" title=\"It's Only the Himalayas\">It's Only the Himalayas</a>",
"<a href=\"catalogue/page-2.html\">next</a>"
]